//#include <Binary.h> 
//#include <Sprite.h> 
//#include <Matrix.h> 
#include "LedControl.h"
#include "WProgram.h"
void setup();
void loop();
LedControl lc=LedControl(12,11,10,1);

int arr1[8];
int arr2[8];
int arr3[8];
int arr4[8];

int arr5[8];
int arr6[8];
int arr7[8];
int arr8[8];

int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;

int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
//	          Matrix(data, clock, load)
//Matrix myMatrix = Matrix(12, 11, 10);

void setup(){
  //myMatrix.clear(); // clear display 
  
  Serial.begin(9600);

  for(int i=2;i<=9;i++){
    pinMode(i,INPUT);
  }
  for(int j=14;j<=69;j++){
    pinMode(j,INPUT);
  }
  
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  
 for(int j=0;j<8;j++){
  for(int i=0;i<8;i++){
   lc.setLed(0,j,i,true);
   delay(10);  
   lc.setLed(0,j,i,false);
  } 
  }
 
  pinMode(13,OUTPUT);
}

void loop(){
  
 
  
  digitalWrite(13,HIGH);
  
  for(int i=0;i<8;i++){
    //arr1[i] = digitalRead(i+4);  //4~11
    
    switch (i){  // 19~14 + 2,3
       case 0:
       arr8[0] = digitalRead(63);  //56~63  
       arr1[0] = digitalRead(4);
       arr2[0] = digitalRead(19);
       arr3[0] = digitalRead(25);
       arr7[0] = digitalRead(55);
       break;
       case 1:
       arr8[1] = digitalRead(62);
       arr1[1] = digitalRead(5);
       arr2[1] = digitalRead(18);
       arr3[1] = digitalRead(24);
       arr7[1] = digitalRead(54);
       break;
       case 2:
       arr8[2] = digitalRead(61);
       arr1[2] = digitalRead(6);
       arr2[2] = digitalRead(17);
       arr3[2] = digitalRead(23);
       arr7[2] = digitalRead(67);
       break;
       case 3:
       arr8[3] = digitalRead(60);
       arr1[3] = digitalRead(7);
       arr2[3] = digitalRead(16);
       arr3[3] = digitalRead(22);
       arr7[3] = digitalRead(66);
       break;
       case 4:
       arr8[4] = digitalRead(59);
       arr1[4] = digitalRead(8);
       arr2[4] = digitalRead(15);
       arr3[4] = digitalRead(64);
       arr7[4] = digitalRead(53);
       break;
       case 5:
       arr8[5] = digitalRead(58);
       arr1[5] = digitalRead(9);
       arr2[5] = digitalRead(14);
       arr3[5] = digitalRead(65);
       arr7[5] = digitalRead(52);
       break;
       case 6:
       arr8[6] = digitalRead(57);
       arr1[6] = digitalRead(69);
       arr2[6] = digitalRead(2);
       arr3[6] = digitalRead(21);
       arr7[6] = digitalRead(51);
       break;
       case 7:
       arr8[7] = digitalRead(56);
       arr1[7] = digitalRead(68);
       arr2[7] = digitalRead(3);
       arr3[7] = digitalRead(20);
       arr7[7] = digitalRead(50);
       break;
    }

     arr4[i] = digitalRead(33-i);  //26~33
     arr5[i] = digitalRead(41-i);  //34~41
     arr6[i] = digitalRead(49-i);  //42~49
     
  }  
  

  for(int i=0;i<=7;i++) {
    
    switch(i){
     case 0:
     a1 += arr1[0] * 128;
     a2 += arr2[0] * 128;
     a3 += arr3[0] * 128;
     a4 += arr4[0] * 128;
     a5 += arr5[0] * 128;
     a6 += arr6[0] * 128;
     a7 += arr7[0] * 128;
     a8 += arr8[0] * 128;
     break;
     case 1:
     a1 += arr1[1] * 64;
     a2 += arr2[1] * 64;
     a3 += arr3[1] * 64;
     a4 += arr4[1] * 64;
     a5 += arr5[1] * 64;
     a6 += arr6[1] * 64;
     a7 += arr7[1] * 64;
     a8 += arr8[1] * 64;
     break;
     case 2:
     a1 += arr1[2]* 32;
     a2 += arr2[2]* 32;
     a3 += arr3[2]* 32;
     a4 += arr4[2]* 32;
     a5 += arr5[2]* 32;
     a6 += arr6[2]* 32;
     a7 += arr7[2]* 32;
     a8 += arr8[2]* 32;
     break;
     case 3:
     a1 += arr1[3]* 16;
     a2 += arr2[3]* 16;
     a3 += arr3[3]* 16;
     a4 += arr4[3]* 16;
     a5 += arr5[3]* 16;
     a6 += arr6[3]* 16;
     a7 += arr7[3]* 16;
     a8 += arr8[3]* 16;
     break;
     case 4:
     a1 += arr1[4]* 8;
     a2 += arr2[4]* 8;
     a3 += arr3[4]* 8;
     a4 += arr4[4]* 8;
     a5 += arr5[4]* 8;
     a6 += arr6[4]* 8;
     a7 += arr7[4]* 8;
     a8 += arr8[4]* 8;
     break;
     case 5:
     a1 += arr1[5]* 4;
     a2 += arr2[5]* 4;
     a3 += arr3[5]* 4;
     a4 += arr4[5]* 4;
     a5 += arr5[5]* 4;
     a6 += arr6[5]* 4;
     a7 += arr7[5]* 4;
     a8 += arr8[5]* 4;
     
     break;
     case 6:
     a1 += arr1[6]* 2;
     a2 += arr2[6]* 2;
     a3 += arr3[6]* 2;
     a4 += arr4[6]* 2;
     a5 += arr5[6]* 2;
     a6 += arr6[6]* 2;
     a7 += arr7[6]* 2;
     a8 += arr8[6]* 2;
     break;
     case 7:
     a1 += arr1[7]* 1;
     a2 += arr2[7]* 1;
     a3 += arr3[7]* 1;
     a4 += arr4[7]* 1;
     a5 += arr5[7]* 1;
     a6 += arr6[7]* 1;
     a7 += arr7[7]* 1;
     a8 += arr8[7]* 1;
     break;
    }
    
  }


 
 //for(int i=0;i<8;i++){
   lc.setRow(0,0,a1);
   lc.setRow(0,1,a2);
   lc.setRow(0,2,a3);
   lc.setRow(0,3,a4);
   lc.setRow(0,4,a5);
   lc.setRow(0,5,a6);
   lc.setRow(0,6,a7);
   lc.setRow(0,7,a8);
   //delay(100);  
  //} 
  //lc.setRow(0,j,B0);
   
 //}
  
//================debug================

 /*Serial.print("Row1:");
  for(int i=0;i<8;i++){
   Serial.print(arr1[i]); 
  }
  Serial.println();
  */
  Serial.print("a8:");
  Serial.println(a8);
  a1 = 0;
  a2 = 0;
  a3 = 0;
  a4 = 0;
  a5 = 0;
  a6 = 0;
  a7 = 0;
  a8 = 0;
    
  /* Serial.print("Row2:");
  for(int i=0;i<8;i++){
   Serial.print(arr2[i]); 
  }
  Serial.println();
  
  Serial.print("Row3:");
  for(int i=0;i<8;i++){
   Serial.print(arr3[i]); 
  }
  Serial.println();
  
  Serial.print("Row4:");
  for(int i=0;i<8;i++){
   Serial.print(arr4[i]); 
  }
  Serial.println();
  
  Serial.print("Row5:");
  for(int i=0;i<8;i++){
   Serial.print(arr5[i]); 
  }
  Serial.println();
  
   Serial.print("Row6:");
  for(int i=0;i<8;i++){
   Serial.print(arr6[i]); 
  }
  Serial.println();
  
   Serial.print("Row7:");
  for(int i=0;i<8;i++){
   Serial.print(arr7[i]); 
  }
  Serial.println();
  
  Serial.print("Row8:");
  for(int i=0;i<8;i++){
   Serial.print(arr8[i]); 
  }
  Serial.println();*/
}

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

